﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SksApp
{
    public partial class MagazaTanimView : BaseView
    {
        public MagazaTanimView()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Event.Invoke(null, null);
        }

        private void MagazaTanimView_Load(object sender, EventArgs e)
        {
            Liste();
            cmbOnaySekli.SelectedIndex = 0;
        }

        private void Liste()
        {
            using (SksEntity ent = new SksEntity())
            {
                listBox1.DataSource = ent.Magazalar.ToList();
            }
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtKodu.Text) && !string.IsNullOrEmpty(txtFisLimiti.Text) && !string.IsNullOrEmpty(txtAdi.Text))
            {
                using (SksEntity ent = new SksEntity())
                {
                    int no = txtKodu.Text.ToInt();
                    var mg = ent.Magazalar.Where(x => x.No == no).FirstOrDefault();
                    if (mg != null)
                    {
                        mg.OnaySekli = Cevir.Parse<byte>(cmbOnaySekli.SelectedIndex);
                        mg.Adi = txtAdi.Text;
                        mg.FisLimiti = Cevir.Parse<decimal>(txtFisLimiti.Text);
                    }
                    else
                    {
                        mg = new Magaza();
                        mg.No = txtKodu.Text.ToInt();
                        mg.OnaySekli = Cevir.Parse<byte>(cmbOnaySekli.SelectedIndex);
                        mg.Adi = txtAdi.Text;
                        mg.FisLimiti = Cevir.Parse<decimal>(txtFisLimiti.Text);
                        ent.Magazalar.Add(mg);
                    }
                    ent.SaveChanges();
                }
                Liste();
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                Magaza mg = (Magaza)listBox1.SelectedItem;
                txtAdi.Text = mg.Adi;
                txtFisLimiti.Text = mg.FisLimiti.ToString();
                txtKodu.Text = mg.No.ToString();
                cmbOnaySekli.SelectedIndex = mg.OnaySekli;
            }
        }

        private void btnSil_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtKodu.Text) && !string.IsNullOrEmpty(txtFisLimiti.Text) && !string.IsNullOrEmpty(txtAdi.Text))
            {
                using (SksEntity ent = new SksEntity())
                {
                    int no = txtKodu.Text.ToInt();
                    var mg = ent.Magazalar.Where(x => x.No == no).FirstOrDefault();
                    if (mg != null)
                    {
                        ent.Magazalar.Remove(mg);
                    }
                    ent.SaveChanges();
                }
                Liste();
            }
        }
    }
}
